// File: LoginView.java

package view;

import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import model.Flight;
import model.QueryModel;

/**
 * Creates a view to delete user.
 *
 * @author Tobias Robinson
 */
public class DeleteFlightView extends JDialog implements ActionListener {

    private ArrayList<String> flights;
    private Object[] flightItems = null;
    private JList flightList;
    private JComboBox flightsCombo;
    private QueryModel query;

    //components of the login window
    private JLabel idLabel = new JLabel("Select Flight ID:  ");
    private JButton deleteButton = new JButton("Delete");

    /**
     * Creates a view to login user.
     *
     * @param parent tells view to run over main window
     */
    public DeleteFlightView(JFrame parent) throws SQLException {
        super(parent, "Delete Flight", true);
        setResizable(false);

        //content pane for login window
        JPanel loginPane = new JPanel();
        setContentPane(loginPane);

        //user GridBag layout
        GridBagLayout gb = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        loginPane.setLayout(gb);
        loginPane.setBorder(BorderFactory.createEmptyBorder(12, 12, 11, 11));

        // place the username label
        c.gridwidth = GridBagConstraints.RELATIVE;
        c.insets = new Insets(10, 10, 5, 0);
        gb.setConstraints(idLabel, c);
        loginPane.add(idLabel);

        // create a combo box
        flights = Flight.getFlights();
        flightItems = flights.toArray();
        flightsCombo = new JComboBox(flightItems);
        flightsCombo.setEditable(false);

        // create a list with the same data model
        flightList = new JList(flightsCombo.getModel());

        //add combo box to panel
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.insets = new Insets(10, 0, 5, 10);
        gb.setConstraints(flightsCombo, c);
        loginPane.add(flightsCombo);

        // place the delete button
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.insets = new Insets(10, 10, 5, 10);
        c.anchor = GridBagConstraints.CENTER;
        gb.setConstraints(deleteButton, c);
        loginPane.add(deleteButton);

        // end of layout

        // Register password field and OK button with action event handler.
        // An action event is generated when the return key is pressed while
        // the cursor is in the password field or when the OK button is pressed.
        deleteButton.addActionListener(this);
    }

    /**
     * When user click button or presses enter processes information
     *
     * @param e event type
     */
    public void actionPerformed(ActionEvent e) {
        String id = flightsCombo.getSelectedItem().toString();
        Flight.deleteFlight(id);
        dispose();
    }
}
